﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
namespace Free2AskModel
{
    public static class Helpers
    {
        //public static List<Post> FindQuestions(this ObjectSet<Post> posts)
        //{
        //    return (from p in posts
        //           where p.QuestionID == null
        //           select p).ToList();
        //}

        public static PostType GetPostType(this Post post)
        {
            Free2AskModel.Free2AskDBEntities db = new Free2AskDBEntities();
            var questions=from p in db.Posts.OfType<Question>()//.Single(p => p.ID == post.ID);
                      where p.ID==post.ID
                      select p;
            if (questions.Count() != 0)
            {
                return PostType.Question;
            }
            else
            {
                var answers = from p in db.Posts.OfType<Answer>()
                        where p.ID == post.ID
                        select p;
                if (answers.Count() != 0)
                {
                    return PostType.Answer;
                }
                else
                {
                    throw new Exception("Cannot determine Post Type.");
                }
            }
        }

        public static int TagFrequency(this Tag tag)
        {
            
            return tag.Posts.Count();
        }

    }

    public enum PostType{Question,Answer}
}
